package com.docker.core.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import kit.docker.exception.BaseException;
import kit.docker.web.APIResponse;
import org.apache.log4j.Logger;

public class ExceptionInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation inv) {
        try {
            inv.invoke();
        } catch (Exception e) {
            if (e instanceof BaseException) {
                APIResponse apiRes = APIResponse.fail(((BaseException) e).getStatus(), ((BaseException) e).getData(), e.getMessage());
                inv.getController().renderJson(apiRes);
            } else {
                Logger.getLogger(ExceptionInterceptor.class).error("Exception:系统服务异常", e);
                e.printStackTrace();
                inv.getController().renderJson(APIResponse.failMessage("系统服务异常"));
            }
        }
    }
}
